<?php
namespace app\admin\controller;

use think\Config;
use think\Session;
use app\admin\model\User;

class Index extends Authentication
{
    //初始化-模板参数配置
    public function _initialize()
    {
        $this->assignConfig();
    }
	  //模板参数赋值函数
    public function assignConfig()
    { 
        //操作系统配置信息
        $server_os = PHP_OS;
        $server_os_version = php_uname('v');
        
        //服务器域名
        $server_domain = $_SERVER['SERVER_NAME'];
        
        //获取服务器IP
        if('/'==DIRECTORY_SEPARATOR){
          $server_ip = $_SERVER['SERVER_ADDR'];
        }else{
          $server_ip = @gethostbyname($_SERVER['SERVER_NAME']);
        }
        //运行环境
        $server_software = $_SERVER['SERVER_SOFTWARE'];
        
        //当前时间
        $server_time = date('Y年m月d日 H:i:s');
        
        //管理员信箱
        $server_admin = $_SERVER['SERVER_ADMIN'];
        
        //上传文件限制
        $upload_max_filesize = get_cfg_var("upload_max_filesize");
        
        //内存使用限制
        $memory_limit = get_cfg_var("memory_limit");
        
        //SMTP支持
        $smtp_support = get_cfg_var("SMTP")?'<font color="green">√</font>' : '<font color="red">×</font>';
        
        //curl开启状态
        //$curl_support = $this->isfun("curl_init");
        
        //exif开启状态
        //$curl_support = $this->isfun("curl_init");
        
        //脚本超时
        //$max_execution_time = $this->show("max_execution_time");
        
        //PHP系统版本
        $php_version = PHP_VERSION;
        
        //运行环境
        $php_sapi_name = php_sapi_name();
        
        //查询数据
        $mysql_version = $this->_mysql_version();
        
        //今日登录用户数
        //$user = new User;
        $loginUsers = User::getDairyLoginUsers();
        
        //将配置信息及环境变量赋值到模板
        $this->assign('SERVER_OS',$server_os);
        $this->assign('SERVER_OS_VERSION',$server_os_version);
        $this->assign('SERVER_DOMAIN',$server_domain);
        $this->assign('HOST_IP',$server_ip);
        $this->assign('SERVER_SOFTWARE',$server_software);
        $this->assign('PHP_VERSION',$php_version);
        $this->assign('PHP_SAPI_NAME',$php_sapi_name);
        $this->assign('PHP_SERVER_TIME',$server_time);
        $this->assign('SERVER_ADMIN',$server_admin);
        $this->assign('UPLOAD_MAX_FILESIZE',$upload_max_filesize);
        $this->assign('MEMORY_LIMIT',$memory_limit);
        $this->assign('SMTP_SUPPORT',$smtp_support);
        $this->assign('MYSQL_VER',$mysql_version);
        //$this->assign('CURL_SUPPORT',$curl_support);
        
        $this->assign('Dairy_Login',$loginUsers);
        
        //用户身份类型
        $user_type = Session::get('user_type');
        $this->assign('USER_TYPE',$user_type);
    }
	  
    //系统用户界面加载
    public function index()
    {   
      if(request()->isGet()){
        
        //获取身份类型
        $type = (int)input('user_type');
        
        //根据身份类型跳转不同登录页面
        switch($type){
          case config('ADMINISTRATOR'):
            return $this->fetch('index');
            break;
          case config('EDITOR'):
            return $this->fetch('editor');
            break;
          case config('WRITER'):
            return $this->fetch('writer');
            break;
          case config('CONTRIBUTOR'):
            return $this->fetch('contributor');
            break;
          case config('SUBSCRIBER'):
            return $this->fetch('subscriber');
            break;
          default:
            return $this->error('用户身份异常',url('Login/index'));
            break;
        }
      }
    }

    //函数支持检测
    private function isfun($funName = '')
    {
      if (!$funName || trim($funName) == '' || preg_match('~[^a-z0-9\_]+~i', $funName, $tmp)) return '错误';
      return (false !== function_exists($funName)) ? '<font color="green">√</font>' : '<font color="red">×</font>';
    }
    //获取 PHP 配置选项的值
    private function show($varName)
    {
      switch($result = get_cfg_var($varName))
      {
        case false:
          return '<font color="red">×</font>';
          break;
        case 1:
          return '<font color="green">√</font>';
          break;
        default:
          return $result;
          break;
      }
    }
    private function _mysql_version()
    {
        //mysql版本查询
        $version = \think\Db::query('select version() as ver');
        return $version[0]['ver'];
    }
}